<template>
    <div>
        <Modal :title="modalTitle" v-model="modalShow" :width="modalWidth" :mask-closable="false" :closable="false">
            <div v-show="modalType === 'edit' || modalType === 'add'">
                <Form ref="formItem" :rules="ruleValidate" label-position="right" :model="formItem" :label-width="130">
                    <FormItem label="文章类别：" prop="category_id">
                        <Select v-model="formItem.category_id" placeholder="请选择文章类别" filterable>
                            <Option v-for="(option, index) in categoryObj" :value="option.id" :key="index">{{option.category}}</Option>
                        </Select>
                    </FormItem>
                    <FormItem label="文章标题：" prop="title">
                        <Input type="text" v-model="formItem.title" placeholder="请输入文章标题" clearable></Input>
                    </FormItem>
                    <FormItem label="作者名称：" prop="author">
                        <Input type="text" v-model="formItem.author" placeholder="请输入文章作者" clearable></Input>
                    </FormItem>
                    <FormItem label="作者邮箱：" prop="author_email">
                        <Input type="text" v-model="formItem.author_email" placeholder="请输入邮箱" clearable></Input>
                    </FormItem>
                    <FormItem label="缩略图路径：" prop="thumb">
                        <Input type="text" v-model="formItem.thumb" placeholder="请输入缩略图路径" clearable></Input>
                    </FormItem>
                    <FormItem label="跳转路径：" prop="link">
                        <Input type="text" v-model="formItem.link" placeholder="请输入跳转路径" clearable></Input>
                    </FormItem>
                    <FormItem label="预发布时间：" prop="publish_time">
                        <DatePicker type="date" v-model="formItem.publish_time" placeholder="请选择预发布时间" style="width: 200px"></DatePicker>
                    </FormItem>
                    <FormItem label="关键字：" prop="keywords">
                        <Input type="text" v-model="formItem.keywords" placeholder="请输入关键字" clearable></Input>
                    </FormItem>
                    <FormItem label="文章摘要：" prop="description">
                        <Input type="textarea" :autosize="{minRows: 2}" v-model="formItem.description" placeholder="请输入文章摘要" clearable></Input>
                    </FormItem>
                    <FormItem label="文章内容：">
                        <wl-vue2-editor v-model="formItem.content"></wl-vue2-editor>
                    </FormItem>
                </Form>
            </div>
            <div v-show="modalType === 'info'">
                <Form label-position="right" :model="formItem" :label-width="150">
                    <FormItem label="文章类别：">
                        {{formItem.category}}
                    </FormItem>
                    <FormItem label="文章标题：">
                        {{formItem.title}}
                    </FormItem>
                    <FormItem label="作者名称：">
                        {{formItem.author}}
                    </FormItem>
                    <FormItem label="作者邮箱：">
                        {{formItem.author_email}}
                    </FormItem>
                    <FormItem label="缩略图：">
                        {{formItem.thumb}}
                    </FormItem>
                    <FormItem label="跳转路径：">
                        {{formItem.link}}
                    </FormItem>
                    <FormItem label="预发布时间：">
                        {{formItem.publish_time}}
                    </FormItem>
                    <FormItem label="关键字：">
                        {{formItem.keywords}}
                    </FormItem>
                    <FormItem label="文章摘要：">
                        {{formItem.description}}
                    </FormItem>
                    <FormItem label="文章内容：">
                        <!--<pre><code>{{formItem.content}}</code></pre>-->
                        <div v-html="formItem.content">{{formItem.content}}</div>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <template v-if="modalType === 'edit' || modalType === 'add'">
                    <Button type="text" @click="cancelOption">取消</Button>
                    <Button type="success" @click="saveOption" :loading="btnLoading">保存</Button>
                </template>
                <template v-if="modalType === 'info'">
                    <Button type="text" @click="cancelOption">关闭</Button>
                </template>
            </div>
        </Modal>
    </div>
</template>

<script>
import {
  getArticleCategoryList,
  saveArticle
} from '@/api/publish-info'
import { formatDate } from '@/components/dateFormat'
export default {
  name: 'optionModal',
  props: {
    modalTitle: {
      type: String,
      default: () => {
        return ''
      }
    },
    modalType: {
      type: String,
      default: () => {
        return 'info'
      }
    },
    modalShow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    modalWidth: {
      type: Number,
      default: () => {
        return 800
      }
    },
    dataObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      ruleValidate: {
        category_id: [
          { required: true, message: '文章类型不能为空', trigger: 'change' }
        ],
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        author: [
          { required: true, message: '文章作者不能为空', trigger: 'blur' }
        ],
        author_email: [
          { required: true, message: '作者邮箱不能为空', trigger: 'blur' }
        ],
        thumb: [
          { required: true, message: '缩略图不能为空', trigger: 'blur' }
        ],
        link: [
          { required: true, message: '跳转路径不能为空', trigger: 'blur' }
        ],
        keywords: [
          { required: true, message: '文章关键字不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '文章摘要不能为空', trigger: 'blur' }
        ]
      },
      btnLoading: false,
      formItem: {
        content: ''
      },
      categoryObj: {}
    }
  },
  methods: {
    getArticleCategoryList () {
      getArticleCategoryList().then(ret => {
        let data = ret.data
        if (data.errcode === 0) {
          this.categoryObj = ret.data.data
        } else {
          this.error(data)
        }
      })
    },
    cancelOption () {
      this.$emit('cancelOption')
    },
    saveOption () {
      let param = {}
      if (this.modalType === 'add') {
        param = this.formItem
        this.$delete(param, 'id')
      } else if (this.modalType === 'edit') {
        param = this.formItem
      }
      this.$refs['formItem'].validate((valid) => {
        if (valid) {
          this.formItem.publish_time = formatDate(this.formItem.publish_time, 'yyyy-MM-dd')
          // console.log('提交表单', this.formItem)
          this.btnLoading = true
          saveArticle(param).then(ret => {
            let data = ret.data
            if (data.errcode === 0) {
              this.btnLoading = false
              this.success()
              this.$emit('saveOption')
            } else {
              this.btnLoading = false
              this.error(data)
            }
          })
        } else {
          this.$Message.error({
            content: '操作失败,请核对添加信息后重试'
          })
        }
      })
    },
    success (val) {
      val = this.$common.xeUtils.isEmpty(val) ? '操作成功' : val
      this.$Message.success(val)
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    }
  },
  mounted () {
    this.getArticleCategoryList()
  },
  watch: {
    dataObj (val) {
      this.formItem = val
    }
  },
  computed: {}
}
</script>

<style scoped>
</style>
